package com.tyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyg.entity.RoleMenu;
import com.tyg.mapper.RoleMenuMapper;
import com.tyg.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 权限（角色菜单关联）表 服务实现类
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-10-22
 */
@Service
@Transactional
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Autowired
    RoleMenuMapper rmMapper;

    //更新角色菜单
    @Override
    public void updateRoleMenu(int roleId,int [] menuIds) {
        //1.先删除该角色所有菜单
        QueryWrapper<RoleMenu> wrapper =new QueryWrapper<RoleMenu>();
        wrapper.eq("roleId",roleId);
        rmMapper.delete(wrapper);

        //2.重新添加该角色菜单
        for(int menuId:menuIds) {
            RoleMenu rm=new RoleMenu();
            rm.setRoleId(roleId);
            rm.setMenuId(menuId);
            rmMapper.insert(rm);
        }
    }

    @Override
    public List<RoleMenu> selectList(QueryWrapper<RoleMenu> queryWrapper) {
        return rmMapper.selectList(queryWrapper);
    }

}
